package com.yan.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ModelDriven;
import com.yan.pojo.Friend;
import com.yan.service.FriendService;

import net.sf.json.JSONArray;

/**
 *@className:FriendAction
 * @author:颜烈
 * @date:2017年12月25日
 * @Discription:联系人action
 */

public class FriendAction implements ModelDriven<Friend> {
	
	private Friend friend = new Friend();
	
	private FriendService friendService;

	public FriendService getFriendService() {
		return friendService;
	}

	public void setFriendService(FriendService friendService) {
		this.friendService = friendService;
	}

	/**
	 * @Discription:ModelDriven注入属性值
	 */
	public Friend getModel() {
		return friend;
	}
	
	/**
	 * @Discription:修改编码
	 * 设置一下编码(注：Action与Servlet息息相关,Action就是封装好的Servlet,只不过Action将Servlet封装到了最底层
	 * 故Servlet中能做的操作,在Action中都能完成)
	 */
	public void setCharset() {
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
	}
	
	/**
	 * @Discription:查询所有联系人
	 * @return
	 * @throws IOException 
	 */
	public String findAll() throws IOException {
		setCharset();
		List<Object> list = friendService.findAllFriends(friend);
		String json = JSONArray.fromObject(list).toString();
		ServletActionContext.getResponse().getWriter().print(json);
		return null;
	}
	
	/**
	 * @Discription:添加联系人
	 * @return:null
	 */
	public String add() {
		friendService.addFriends(friend);
		return null;
	}
	
	/**
	 * @Discription:编辑(查询)
	 * @return null
	 * @throws IOException 
	 */
	public String findOneFriend() throws IOException {
		setCharset();
		List<Friend> list = friendService.selectFriend(friend);
		String json = JSONArray.fromObject(list).toString();
		ServletActionContext.getResponse().getWriter().print(json);
		return null;
	}
	
	/**
	 * @Discription:编辑(修改)
	 * @return:null
	 */
	public String update() {
		System.out.println(friend.getF_id() + "," + friend.getF_username() + "," + friend.getF_age());
		friendService.updateFriend(friend);
		return null;
	}
	
	/**
	 * @Discription:删除
	 * @return:null
	 */
	public String delete() {
		friendService.deleteFriend(friend);
		return null;
	}
	
	/**
	 * @Discription:按名字模糊查询
	 * @return:null
	 * @throws IOException 
	 */
	public String blurSelectByName() throws IOException {
		setCharset();
		List<Object> list = friendService.blurSelectForName(friend);
		String json = JSONArray.fromObject(list).toString();
		ServletActionContext.getResponse().getWriter().print(json);
		return null;
	}
	
	/**
	 * @Dicription:按手机号码查询
	 * @return:null
	 * @throws IOException 
	 */
	public String blurSelectByPhone() throws IOException {
		setCharset();
		List<Object> list = friendService.blurSelectForPhone(friend);
		if (list.size() > 0) {
			System.out.println("查询成功...");
			String json = JSONArray.fromObject(list).toString();
			ServletActionContext.getResponse().getWriter().print(json);
		} else {
			System.out.println("查询失败...");
		}
		return null;
	}
	
	public String deleteList() {
		HttpServletRequest req = ServletActionContext.getRequest();
		String value[] = req.getParameterValues("deleteBox");
		friendService.deleteListById(value, friend);
		return null;
	}
	
}
